package com.narunas;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore.Images;
import android.util.Log;


public class sdSaver 
{
	public boolean saveImage(InputStream is, String filename) throws InterruptedException{
		
		 InputStream inputStream = null;
		 OutputStream outStream = null;
		
		try{
			inputStream = is;
			String filepath=Environment.getExternalStorageDirectory().getAbsolutePath() + "NOAAWX/";
			  
			 File imageDirectory = new File(filepath);
			 File file = new File(filepath + filename);
			 
			 /*
			 
			 ContentValues values = new ContentValues();
			  values.put(Images.Media.TITLE, filename);
			  values.put(Images.Media.DISPLAY_NAME, filename);
			  values.put(Images.Media.MIME_TYPE, "image/*");
			  //values.put(Images.Media.INTERNAL_CONTENT_URI, filepath);
			  values.put(Images.ImageColumns.BUCKET_ID, filepath.hashCode());
			  values.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, "TODAY");
			  
			  //ContentResolver contentResolver = Context; //getApplicationContext().getContentResolver();
			  	  
			  
			  //Uri uri = contentResolver.insert(Images.Media.INTERNAL_CONTENT_URI, values);
			  
			  //outStream = contentResolver.openOutputStream(uri);
			 
			 */
			 
			 OutputStream out = new FileOutputStream(file);
			 
			  byte[] buffer = new byte[1024];
			 int count;
			 
			 
			 while ((count = inputStream.read(buffer)) > 0) {
				    if (Thread.interrupted() == true) {
				     String functionName = Thread.currentThread().getStackTrace()[2].getMethodName() + "()";
				    throw new InterruptedException("The function " + functionName + " was interrupted.");
				    }
				   
				    out.write(buffer, 0, count);
			 }
			 
		
			 
			 out.close();
			 is.close();
			 
			 Log.d("WXDroid", "file saved: "+ filepath + " " + filename);
			 return true;
			  /*
			  if (file.exists() == false) {
				   String path = imageDirectory.toString().toLowerCase();
				   String name = imageDirectory.getName().toLowerCase();
				   ContentValues values = new ContentValues(7);
				   values.put(Images.Media.TITLE, filename);
				   values.put(Images.Media.DISPLAY_NAME, filename);
				   values.put(Images.Media.DATE_TAKEN, new Date().getTime());
				   values.put(Images.Media.MIME_TYPE, "image/jpeg");
				   values.put(Images.ImageColumns.BUCKET_ID, path.hashCode());
				   values.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, name);
				   values.put("_data", filepath + filename);
				   ContentResolver contentResolver = getApplicationContext().getContentResolver();
				   Uri uri = contentResolver.insert(Images.Media.EXTERNAL_CONTENT_URI, values);
				   outStream = contentResolver.openOutputStream(uri);
				   byte[] buffer = new byte[1024];
				   int count;
				   while ((count = inputStream.read(buffer)) != -1) {
				    if (Thread.interrupted() == true) {
				     String functionName = Thread.currentThread().getStackTrace()[2].getMethodName() + "()";
				     throw new InterruptedException("The function " + functionName + " was interrupted.");
				    }
				    outStream.write(buffer, 0, count);
				   }
				  }
			  
			  */
			
		} catch(IOException e){
			
			return false;
		}		
	}
	
}
